package com.huixh.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huixh.train.business.domain.TrainCarriage;
import com.huixh.train.business.domain.TrainCarriageExample;
import com.huixh.train.business.enums.SeatColEnum;
import com.huixh.train.business.mapper.TrainCarriageMapper;
import com.huixh.train.business.req.TrainCarriageQueryReq;
import com.huixh.train.business.req.TrainCarriageSaveReq;
import com.huixh.train.business.resp.TrainCarriageQueryResp;
import com.huixh.train.business.service.TrainCarriageService;
import com.huixh.train.common.resp.PageResult;
import com.huixh.train.utils.random.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huixh
 */
@Service
public class TrainCarriageServiceImpl implements TrainCarriageService {

    private final Logger logger = LoggerFactory.getLogger(TrainCarriageServiceImpl.class);
    @Resource
    private TrainCarriageMapper trainCarriageMapper;

    @Resource
    private SnowUtil snowUtil;

    @Override
    public void save(TrainCarriageSaveReq trainCarriageSaveReq) {
        List<SeatColEnum> colEnums = SeatColEnum.getColsByType(trainCarriageSaveReq.getSeatType());
        trainCarriageSaveReq.setColCount(colEnums.size());
        trainCarriageSaveReq.setSeatCount(trainCarriageSaveReq.getRowCount() * colEnums.size());
        DateTime now = DateTime.now();
        TrainCarriage trainCarriage = BeanUtil.copyProperties(trainCarriageSaveReq, TrainCarriage.class);
        if (ObjectUtil.isNull(trainCarriage.getId())) {
            trainCarriage.setId(snowUtil.getSnowflakeToLong());
            trainCarriage.setCreateTime(now);
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.insert(trainCarriage);
        } else {
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.updateByPrimaryKey(trainCarriage);
        }
    }

    @Override
    public PageResult<List<TrainCarriageQueryResp>> queryList(TrainCarriageQueryReq trainCarriageQueryReq) {
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.setOrderByClause("train_code asc, `index` asc");
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();

        if (StrUtil.isNotBlank(trainCarriageQueryReq.getTrainCode())) {
            criteria.andTrainCodeEqualTo(trainCarriageQueryReq.getTrainCode());
        }

        logger.info("查询页码：{}", trainCarriageQueryReq.getPageNum());
        logger.info("每页条数：{}", trainCarriageQueryReq.getPageSize());
        PageHelper.startPage(trainCarriageQueryReq.getPageNum(), trainCarriageQueryReq.getPageSize());
        List<TrainCarriage> trainCarriageList = trainCarriageMapper.selectByExample(trainCarriageExample);

        PageInfo<TrainCarriage> pageInfo = new PageInfo<>(trainCarriageList);
        logger.info("总行数：{}", pageInfo.getTotal());
        logger.info("总页数：{}", pageInfo.getPages());

        List<TrainCarriageQueryResp> list = BeanUtil.copyToList(trainCarriageList, TrainCarriageQueryResp.class);
        return PageResult.success(pageInfo.getTotal(), list);
    }

    @Override
    public void delete(Long id) {
        trainCarriageMapper.deleteByPrimaryKey(id);
    }
    @Override
    public List<TrainCarriage> selectByTrainCode(String trainCode){
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        return trainCarriageMapper.selectByExample(trainCarriageExample);
    }

    private TrainCarriage selectByUnique(String trainCode,Integer index) {
        TrainCarriageExample carriageExample = new TrainCarriageExample();
        carriageExample.createCriteria()
                .andTrainCodeEqualTo(trainCode)
                .andIndexEqualTo(index);
        List<TrainCarriage> trainCarriages = trainCarriageMapper.selectByExample(carriageExample);

        if (CollUtil.isEmpty(trainCarriages)) {
            return null;
        } else {
            return trainCarriages.get(0);
        }
    }
}